﻿using System;
using System.Collections;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Xml.Linq;
using System.Text;
using System.Collections.Generic;

namespace Jy123.Web.home.MiniContent
{
    /// <summary>
    /// $codebehindclassname$ 的摘要说明
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    public class Blog : IHttpHandler
    {
        private int userID;
        public void ProcessRequest(HttpContext context)
        {
            string strUserIdFromCookie = Common.Cookie.getCookie("UserID");
            if (string.IsNullOrEmpty(strUserIdFromCookie))
            {
                context.Response.Redirect("/");
            }
            userID = int.Parse(strUserIdFromCookie);

            StringBuilder sbHtml = new StringBuilder(context.Cache["user_minicontent"].ToString());//sbContent
            string strUserID = context.Request.QueryString["MiniContentUserID"];
            string strBlogUl = string.Empty;
            if (!string.IsNullOrEmpty(strUserID))
            {
                strUserID = strUserID.Substring(0, strUserID.IndexOf("?"));
                strBlogUl = BlogListByUserID(context, Convert.ToInt32(strUserID));
            }
            else
            {
                strBlogUl = FriendListByUserID(context, userID);
            }

            if (strBlogUl.Trim() == "")
            {
                strBlogUl = "暂无动态!";
            }
            sbHtml = sbHtml.Replace("$ContentMini_ul$", strBlogUl);
            context.Response.ContentType = "text/plain";
            context.Response.Write(sbHtml.ToString());
        }
        /// <summary>
        /// 用户的日志列表
        /// </summary>
        /// <param name="context"></param>
        /// <param name="userID">用户ID</param>
        /// <returns>string</returns>
        private string BlogListByUserID(HttpContext context, int userID)
        {
            List<int?> count;
            //用户的日志列表
            Jy123.BLL.User.Blog bllUserBlog = new Jy123.BLL.User.Blog();
            //fUser = dtUserFrdList.Rows[j]["FFriend"].ToString();
            DataTable dtMyBlog = bllUserBlog.BlogList_My(1, 3,userID, out count).Tables[0];
            StringBuilder sbBlogLit = new StringBuilder();//sbli
            string strBlogList = context.Cache["user_minicontent_ul_blog_other"].ToString();
            for (int i = 0; i < dtMyBlog.Rows.Count; i++)
            {
                string str = strBlogList;
                str = str.Replace("$BlogID$", dtMyBlog.Rows[i]["ID"].ToString());
                str = str.Replace("$BlogTitle$", Common.Input.GetSubString(dtMyBlog.Rows[i]["Title"].ToString(), 20));
                str = str.Replace("$PostTime$", dtMyBlog.Rows[i]["PostTime"].ToString());
                str = str.Replace("$BlogContent$", Common.Input.GetSubString(dtMyBlog.Rows[i]["BlogContent"].ToString(),50));
                sbBlogLit.Append(str);
            }
            return sbBlogLit.ToString();
        }
        /// <summary>
        /// 根据用户ID取好友列表
        /// </summary>
        /// <param name="context"></param>
        /// <param name="userID">用户ID</param>
        /// <returns>字符串</returns>
        private string FriendListByUserID(HttpContext context, int userID)
        {
            List<int?> count;
            //日志外层的用户列表
            Jy123.BLL.User.Friend bllUserFrd = new Jy123.BLL.User.Friend();
            StringBuilder sbUl = new StringBuilder();//sbul
            string strUl = context.Cache["user_minicontent_ul_blog"].ToString();
            //string strBlogList = context.Cache["user_minicontent_ul_bloglist"].ToString();
            DataTable dtUserFrdList = bllUserFrd.GetFriendList(1, 10, userID, out count).Tables[0];

            for (int j = 0; j < dtUserFrdList.Rows.Count; j++)
            {
                string stringUl = strUl;
                //根据用户ID判断用户是否有头像,如果没有头像就返回默认头像地址
                string userPortrait = new Jy123.BLL.User.SPace().DefaultPortrait(Convert.ToInt32(dtUserFrdList.Rows[j]["FFriend"]));

                stringUl = stringUl.Replace("$UserID$", dtUserFrdList.Rows[j]["FFriend"].ToString())
                .Replace("$UserName$", dtUserFrdList.Rows[j]["UserName"].ToString())
                .Replace("$Portrait$", userPortrait);
                string fUser = dtUserFrdList.Rows[j]["FFriend"].ToString();

                string bloglist = BlogListByUser(context, fUser);
                stringUl = stringUl.Replace("$MiniContentBlogList$",bloglist );
                if (string.IsNullOrEmpty(bloglist.Trim()))
                    continue;
                sbUl.Append(stringUl);
            }
            return sbUl.ToString();
        }
        /// <summary>
        /// 根据用户ID取日志列表
        /// </summary>
        /// <param name="context"></param>
        /// <param name="fUser">用户ID</param>
        /// <returns>string</returns>
        private string BlogListByUser(HttpContext context, string fUser)
        {
            List<int?> count;
            //用户的日志列表
            Jy123.BLL.User.Blog bllUserBlog = new Jy123.BLL.User.Blog();
            //fUser = dtUserFrdList.Rows[j]["FFriend"].ToString();
            DataTable dtMyBlog = bllUserBlog.BlogList_My(1, 3, Convert.ToInt32(fUser), out count).Tables[0];
            StringBuilder sbBlogLit = new StringBuilder();//sbli
            string strBlogList = context.Cache["user_minicontent_ul_bloglist"].ToString();
            for (int i = 0; i < dtMyBlog.Rows.Count; i++)
            {
                string str = strBlogList;
                str = str.Replace("$BlogID$", dtMyBlog.Rows[i]["ID"].ToString());
                str = str.Replace("$BlogTitle$", Common.Input.GetSubString(dtMyBlog.Rows[i]["Title"].ToString(), 20));
                str = str.Replace("$PostTime$", dtMyBlog.Rows[i]["PostTime"].ToString());
                sbBlogLit.Append(str);
            }
            return sbBlogLit.ToString();
        }
        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}
